//
//  StarView.m
//  EvaluationStar
//
//  Created by 赵贺 on 15/11/26.
//  Copyright © 2015年 赵贺. All rights reserved.
//

#import "StarView.h"

#define imageW  self.bounds.size.width/7

@interface StarView ()
{
    NSString* statrScore;
}

@property (nonatomic, strong) UIView *starBackgroundView;
@property (nonatomic, strong) UIView *starForegroundView;

@end
@implementation StarView

+(void)showStartView:(UIView *)selfView  WithFrame:(CGRect)frame EmptyImage:(NSString *)Empty StarImage:(NSString *)Star  andSelectBlock:(void (^)(NSString *))selectRow;
{
    StarView*start=[[StarView alloc]initWithFrame:frame EmptyImage:Empty StarImage:Star];
    start.SelectBlock=selectRow;
    [selfView addSubview:start];
}

//提供一个初始化方法传入一个选中的图片和一个未选中的图片
- (id)initWithFrame:(CGRect)frame EmptyImage:(NSString *)Empty StarImage:(NSString *)Star{

    self = [super initWithFrame:frame];
    if (self) {
        
        //未选中的图片
        self.starBackgroundView = [self buidlStarViewWithImageName:Empty];
        //选中的图片
        self.starForegroundView = [self buidlStarViewWithImageName:Star];
        
        [self addSubview:self.starBackgroundView];
        
        //启用用户交互
        self.userInteractionEnabled = YES;
        
        /**点击手势*/
        UITapGestureRecognizer *tapGR=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGR:)];
        [self addGestureRecognizer:tapGR];

        /**滑动手势*/
        UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(tapGR:)];
        [self addGestureRecognizer:panGR];

    }
    return self;
    
}

- (UIView *)buidlStarViewWithImageName:(NSString *)imageName
{
    //大小
    CGRect frame = self.bounds;
    //初始化一个view大小跟当前视图一样
    UIView *view = [[UIView alloc] initWithFrame:frame];
    //超出边界的部分切割掉
    view.clipsToBounds = YES;
    //for循环创建图片
    for (int j = 0; j < 5; j ++)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
        imageView.frame = CGRectMake(1.5*j*imageW, 0, imageW, imageW);
        [view addSubview:imageView];
    }
    //返回一个view
    return view;
}

//手势的方法
-(void)tapGR:(UITapGestureRecognizer *)tapGR{

    CGPoint point =[tapGR locationInView:self];
    if (point.x<0) {
        point.x = 0;
    }
    if (point.x>imageW*10) {
        point.x=imageW*10;
    }
    NSInteger X=(NSInteger)point.x/(1.5*imageW);
    _str=[NSString stringWithFormat:@"%d",X+1];
    //回调结果
    self.SelectBlock(_str);
    //设置选中的星星的图片
    self.starForegroundView.frame = CGRectMake(0, 0, (X+1)*1.5*imageW, imageW);
    //添加到当前视图
    [self addSubview:self.starForegroundView];
}




@end
